package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	t_table "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetSMSConfigTable(ctx *context.Context) table.Table {
	smsConfig := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := smsConfig.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("region", "region", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("access key", "access_key_id", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("access key secret", "access_key_secret", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("发送协议", "scheme", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("接收方电话", "phone", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("短信签名", "sign", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("短信模板代码", "template", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("是否启用", "is_active", db.Tinyint).FieldEditAble(t_table.Switch).FieldEditOptions(types.FieldOptions{
		{Value: "1", Text: "启用"}, // 放在第一个代表 on
		{Value: "0", Text: "禁用"},
	}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Value: "1", Text: "启用"}, // 放在第一个代表 on
			{Value: "0", Text: "禁用"},
		})
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("更新时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("sms_config").SetTitle("任务记录").SetDescription("SMSConfig")

	formList := smsConfig.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate()
	formList.AddField("region", "region", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("access key", "access_key_id", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("access key secret", "access_key_secret", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("发送协议", "scheme", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("接收方电话", "phone", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("短信签名", "sign", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("短信模板代码", "template", db.Varchar, form.Text).FieldTrimSpace()
	formList.AddField("是否启用", "is_active", db.Tinyint, form.Radio).
		// radio的选项，Text代表显示内容，Value代表对应值
		FieldOptions(types.FieldOptions{
			{Text: "启用", Value: "1"},
			{Text: "禁用", Value: "0"},
		}).FieldDefault("1")
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("sms_config").SetTitle("任务记录").SetDescription("SMSConfig")

	return smsConfig
}
